// hooks/useRecordQuestionView.ts
import { useEffect } from "react";
import { recordQuestionViewUsingPost } from "@/api/userController";
import { useSelector } from "react-redux";
import { RootState } from "@/stores";
import { message } from "antd";

/**
 * 记录题目浏览的 Hook
 */
const useRecordQuestionView = (questionId?: number) => {
  const loginUser = useSelector((state: RootState) => state.loginUser);

  useEffect(() => {
    const recordView = async () => {
      if (!questionId || !loginUser?.id) return;

      try {
        await recordQuestionViewUsingPost({ questionId });
        // 可以在这里添加成功后的回调，比如更新本地状态等
      } catch (error) {
        console.error("记录浏览失败:", error);
        // 对于未登录用户，不显示错误消息
        // if (error.response?.status !== 40100) {
        //   message.error("记录浏览失败");
        // }
      }
    };
    recordView();
  }, [questionId, loginUser?.id]);
};

export default useRecordQuestionView;
